package com.ly.lypicturebackend.manager.webSocket.disruptor;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.ly.lypicturebackend.manager.webSocket.model.PictureEditRequestMessage;
import com.ly.lypicturebackend.model.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import javax.annotation.Resource;

/**
 * 消息生产（生产者）
 * @author luyi
 * @create 2025-03-03-23:50
 */
@Component
@Slf4j
public class PictureEditEventProducer {
    @Resource
    Disruptor<PictureEditEvent> pictureEditEventDisruptor;

    public void publish(PictureEditRequestMessage pictureEditRequestMessage, WebSocketSession session, User user, Long pictureId){
        RingBuffer<PictureEditEvent> ringBuffer = pictureEditEventDisruptor.getRingBuffer();
        long next = ringBuffer.next();
        PictureEditEvent pictureEditEvent = ringBuffer.get(next);
        pictureEditEvent.setPictureEditRequestMessage(pictureEditRequestMessage);
        pictureEditEvent.setSession(session);
        pictureEditEvent.setUser(user);
        pictureEditEvent.setPictureId(pictureId);
        ringBuffer.publish(next);
    }
}
